import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Image,
    Dimensions,
    ListView,
    TouchableOpacity,
    TextInput,
    AsyncStorage
} from 'react-native';
import Config from '../../../Config'
import Utils from '../../utli/Utils'
import {Toast,ModalIndicator} from 'teaset';
const STORAGE_KEY_TOKEN = '@AsyncStorageDemo:key_token';
const {height, width} = Dimensions.get('window');
export default class RemarksPage extends Component {
    componentDidMount() {
        this._loadInitialState().done();
    }
    async _loadInitialState(){
        try{
            let value=await AsyncStorage.getItem(STORAGE_KEY_TOKEN);
            if(value!==null){
                this._appendMessage(value);
            }else{
                this._appendMessage('');
            }
        }catch(error){
            this._appendMessage('AsyncStorage错误'+error.message);
        }
    }
    _appendMessage=(message)=>{
        this.setState({
            token:this.state.token.concat(message)
        },()=>this.getInfo());
    };
    getInfo=()=>{
        let params={
            "token":this.state.token,
        };
        ModalIndicator.show("正在请求数据......");
        fetch(Config.domain+'/user/getInfo',{
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: Utils.getParamsString(params)
        })
            .then((response) => response.json())
            .then((responseJson) => {
                ModalIndicator.hide();
                if(responseJson.status!==1){
                    this.setState({
                        remarkName:responseJson.data.nickname,
                    })
                }else{
                    Toast.message(responseJson.msg);
                }
            })
            .catch((error) => {
                ModalIndicator.hide();
                Toast.message(error);
            });
    };
    constructor(props) {
        super(props);
        this.state = {
            remarkName:'',
            token:'',
            phone:''
        }
    };

    render() {
        return (
            <View style={styles.container}>
                <View style={styles.titleView}>
                    <TouchableOpacity style={styles.goBackView}>
                        <Image
                            source={require('../../img/whiteGoBack.png')}
                            style={styles.goBackImg}/>
                    </TouchableOpacity>
                    <Text style={styles.titleMsg}>设置标签及备注</Text>
                    <TouchableOpacity style={styles.completeView}>
                        <Text style={styles.titleMsg}>完成</Text>
                    </TouchableOpacity>
                </View>
                <View style={styles.iconView}>
                    <Text style={styles.iconMsg}>备注名</Text>
                </View>
                <View style={styles.labelView}>
                    <TextInput
                        ref={ (ref)=>this.remarkNameTextInput = ref }
                        autoCapitalize={"none"}
                        defaultValue={this.state.remarkName}
                        autoCorrect={false}
                        style={styles.textInput}
                        onChangeText={(text)=>{
                            this.setState({
                                remarkName:text,
                            })
                        }}/>
                </View>
                <View style={styles.iconView}>
                    <Text style={styles.iconMsg}>标签</Text>
                </View>
                <TouchableOpacity style={styles.labelView}>
                    <Text style={styles.sortMsg}>通过标签给联系人分类</Text>
                    <Image
                        style={styles.sortImg}
                        source={require('../../../jscode/img/guide.png')}/>
                </TouchableOpacity>
                <View style={styles.iconView}>
                    <Text style={styles.iconMsg}>电话号码</Text>
                </View>
                <View style={styles.labelView}>
                    <TextInput
                        ref={ (ref)=>this.phoneTextInput = ref }
                        autoCapitalize={"none"}
                        defaultValue={this.state.phone}
                        keyboardType={'numeric'}
                        placeholder="添加电话号码"
                        placeholderTextColor='#b2b2b2'
                        autoCorrect={false}
                        style={styles.textInput}
                        onChangeText={(text)=>{
                            this.setState({
                                phone:text,
                            })
                        }}/>
                </View>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor:'#f0f0f0'
    },
    titleView:{
        backgroundColor:'#000000',
        width:width,
        alignItems:'center',
        justifyContent:'center',
        height:64
    },
    titleMsg:{
        textAlign:'center',
        fontSize:17,
        color:'#ffffff',
        marginTop:34,
        marginBottom:29/2
    },
    goBackView:{
        position: 'absolute',
        top:65/2,left:15,
    },
    goBackImg:{
        height:18,
        width:18
    },
    completeView:{
        position: 'absolute',
        right:15,bottom:0
    },
    iconView:{
        height:29,
        width:width,
        justifyContent:'center'
    },
    iconMsg:{
        marginLeft:15,
        fontSize:14,
        color:'#646464'
    },
    labelView:{
        height:45,
        width:width,
        backgroundColor:'#ffffff',
        justifyContent:'center'
    },
    textInput:{
        marginLeft:15,
        width:width-30,
        height:45,
        fontSize:14
    },
    sortMsg:{
        fontSize:14,
        color:'#b2b2b2',
        marginLeft:15
    },
    sortImg:{
        position: 'absolute',
        right:15
    }
});
